package com.xydtech.workflow.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.workflow.domain.WflFlownodeDefine;
import com.xydtech.workflow.service.IWflFlownodeDefineService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程节点定义Controller
 *
 * @author JHON
 * @date 2021-03-16
 */
@Controller
@RequestMapping("/workflow/flownodedefine")
public class WflFlownodeDefineController extends BaseController {
  private String prefix = "workflow/flownodedefine";

  @Autowired private IWflFlownodeDefineService wflFlownodeDefineService;

  @RequiresPermissions("workflow:flownodedefine:view")
  @GetMapping()
  public String flownodedefine() {
    return prefix + "/flownodedefine";
  }

  /** 查询流程节点定义列表 */
  @RequiresPermissions("workflow:flownodedefine:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(WflFlownodeDefine wflFlownodeDefine) {
    startPage();
    List<WflFlownodeDefine> list =
        wflFlownodeDefineService.selectWflFlownodeDefineList(wflFlownodeDefine);
    return getDataTable(list);
  }

  /** 导出流程节点定义列表 */
  @RequiresPermissions("workflow:flownodedefine:export")
  @Log(title = "流程节点定义", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(WflFlownodeDefine wflFlownodeDefine) {
    List<WflFlownodeDefine> list =
        wflFlownodeDefineService.selectWflFlownodeDefineList(wflFlownodeDefine);
    ExcelUtil<WflFlownodeDefine> util = new ExcelUtil<WflFlownodeDefine>(WflFlownodeDefine.class);
    return util.exportExcel(list, "flownodedefine");
  }

  /** 新增流程节点定义 */
  @GetMapping("/add")
  public String add() {
    return prefix + "/add";
  }

  /** 新增保存流程节点定义 */
  @RequiresPermissions("workflow:flownodedefine:add")
  @Log(title = "流程节点定义", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(WflFlownodeDefine wflFlownodeDefine) {
    return toAjax(wflFlownodeDefineService.insertWflFlownodeDefine(wflFlownodeDefine));
  }

  /** 修改流程节点定义 */
  @GetMapping("/edit/{stepId}")
  public String edit(@PathVariable("stepId") Long stepId, ModelMap mmap) {
    WflFlownodeDefine wflFlownodeDefine =
        wflFlownodeDefineService.selectWflFlownodeDefineById(stepId);
    mmap.put("wflFlownodeDefine", wflFlownodeDefine);
    return prefix + "/edit";
  }

  /** 修改保存流程节点定义 */
  @RequiresPermissions("workflow:flownodedefine:edit")
  @Log(title = "流程节点定义", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(WflFlownodeDefine wflFlownodeDefine) {
    return toAjax(wflFlownodeDefineService.updateWflFlownodeDefine(wflFlownodeDefine));
  }

  /** 删除流程节点定义 */
  @RequiresPermissions("workflow:flownodedefine:remove")
  @Log(title = "流程节点定义", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {
    return toAjax(wflFlownodeDefineService.deleteWflFlownodeDefineByIds(ids));
  }
}
